package allinpay

import (
    //"fmt"
	"errors"
	//"wxsdk/utils/mapstruct"
	"github.com/astaxie/beego"
)

type RefundReq struct {
	Cusid     string `form:"cusid"`     //商户号
	Appid     string `form:"appid"`     //应用ID
	Version   string `form:"version"`   //版本号
	Trxamt    string `form:"trxamt"`    //退款金额
	Reqsn     string `form:"reqsn"`     //商户退款订单号
	Oldreqsn  string `form:"oldreqsn"`  //原交易订单号
	Oldtrxid  string `form:"oldtrxid"`  //原交易流水
	Remark    string `form:"remark"`    //备注信息
	Randomstr string `form:"randomstr"` //随机字符串
	Sign      string `form:"sign"`      //签名
}

type RefundRsp struct {
	Retcode   string `json:"retcode"`
	Retmsg    string `json:"retmsg"`
	Cusid     string `json:"cusid"`
	Appid     string `json:"appid"`
	Trxid     string `json:"trxid"`
	Reqsn     string `json:"reqsn"`
	Trxstatus string `json:"trxstatus"`
	Fintime   string `json:"fintime"`
	Errmsg    string `json:"errmsg"`
	Fee       string `json:"fee"`
	Trxcode   string `json:"trxcode"`
	Randomstr string `json:"randomstr"`
	Sign      string `json:"sign"`
}

func Refunds(cusid,appid,account,refundId,oldreqsn,key string)(rsp *RefundRsp, err error){
	req := new(RefundReq)
	req.Cusid = cusid
	req.Appid = appid
	req.Version = "11"
	req.Trxamt = account
	req.Reqsn = refundId
	req.Oldreqsn = oldreqsn
	req.Oldtrxid = ""
	req.Remark = ""
	req.Randomstr = getNonceStr(32)
	reqURL := "https://vsp.allinpay.com/apiweb/unitorder/refund"
	sdk := NewSDK(reqURL, key)
	rsp = new(RefundRsp)
	_, err = sdk.Request(req, rsp)
	if err != nil {
		beego.Debug("Oceanpay Refunds err:", err.Error())
		return
	}
	if rsp.Retcode != "SUCCESS" {
	   err = errors.New(rsp.Retmsg)
	}
	return
}
